package com.ava.avamedia.lottery.miennam;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.ava.avamedia.R;
import com.ava.avamedia.model.MBResult;
import com.ava.avamedia.model.TNResults;
import com.ava.avamedia.util.Constants;
import com.congnt.library.utility.DialogUtils;
import com.congnt.library.utility.HTTPClientUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MNResultsActivity extends Activity {
	public static final String TAG = MNResultsActivity.class.getSimpleName();
	public ProgressDialog dialog;
	private TextView titleTV;
	private TextView specialTV;
	private TextView firstTV;
	private TextView secondTV;
	private TextView thirdTV;
	private TextView fourTV;
	private TextView fifthTV;
	private TextView sixTV;
	private TextView sevenTV;
	private TextView eightTV;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		String url = getIntent().getStringExtra(Constants.URL_RESULT);
		setContentView(R.layout.activity_result_tn);
		titleTV= (TextView) findViewById(R.id.activity_result_tn_title);
		specialTV = (TextView) findViewById(R.id.activity_result_tn_db);
		firstTV = (TextView) findViewById(R.id.activity_result_tn_first);
		secondTV = (TextView) findViewById(R.id.activity_result_tn_second);
		thirdTV = (TextView) findViewById(R.id.activity_result_tn_third);
		fourTV= (TextView) findViewById(R.id.activity_result_tn_four);
		fifthTV = (TextView) findViewById(R.id.activity_result_tn_five);
		sixTV = (TextView) findViewById(R.id.activity_result_tn_six);
		sevenTV = (TextView) findViewById(R.id.activity_result_tn_seven);
		eightTV = (TextView) findViewById(R.id.activity_result_tn_eight);
		if (HTTPClientUtil.isInternetConection()) {
			new LoadResultsAsyncTask().execute(url);
		}
	}
	public void initResult(TNResults mbResult){
		titleTV.setText(mbResult.title);
		specialTV.setText(mbResult.special);
		firstTV.setText(mbResult.first);
		secondTV.setText(mbResult.second);
		thirdTV.setText(mbResult.third);
		fourTV.setText(mbResult.fourth);
		fifthTV.setText(mbResult.fifth);
		sixTV.setText(mbResult.sixth);
		sevenTV.setText(mbResult.seventh);
		eightTV.setText(mbResult.eighth);
	}
	class LoadResultsAsyncTask extends AsyncTask<String, Void, TNResults> {

		@Override
		protected TNResults doInBackground(String... params) {
			String 	url = Constants.URL_KQ + params[0];
			try {
				Document doc = Jsoup.connect(url)
						.userAgent(Constants.USER_AGENT).get();
				Elements elementDay = doc
						.body()
						.select("form#aspnetForm > div.bg_body > div.conten > div.kqxs > div.boder > *");
				Element elementResult = elementDay.get(3);
				return getMBResultByDivTag(elementResult);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = DialogUtils.showProgress(MNResultsActivity.this, "Đang lấy kết quả từ trang web ketquaday.vn....");
		}
		@Override
		protected void onPostExecute(TNResults result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			initResult(result);
		
		}
	}
	public String getResultByElement(Element result) {
		return result.child(1).text();
	}

	public TNResults getMBResultByDivTag(Element elementDiv) {
		TNResults tnResult = new TNResults();
		Elements elementTableResult = elementDiv.child(1)
				.select("table > tbody > tr ").get(0)
				.select("table > tbody > *");
		Element specialResult = elementTableResult.get(8);
		Element firstResult = elementTableResult.get(7);
		Element secondResult = elementTableResult.get(6);
		Element thirdResult = elementTableResult.get(5);
		Element fourthResult = elementTableResult.get(4);
		Element fifthResult = elementTableResult.get(3);
		Element sixthResult = elementTableResult.get(2);
		Element seventhResult = elementTableResult.get(1);
		Element eighthResult = elementTableResult.get(0);
		
		tnResult.title = elementDiv.getElementsByClass("main_table")
				.text();
		tnResult.special = getResultByElement(specialResult);
		tnResult.first = getResultByElement(firstResult);
		tnResult.second = getResultByElement(secondResult);
		tnResult.third = getResultByElement(thirdResult);
		tnResult.fourth = getResultByElement(fourthResult);
		tnResult.fifth = getResultByElement(fifthResult);
		tnResult.sixth = getResultByElement(sixthResult);
		tnResult.seventh = getResultByElement(seventhResult);
		tnResult.eighth = getResultByElement(eighthResult);
		return tnResult;
	}
}
